package response;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/html")
public class HTMLTypeServlet extends HttpServlet {

    //html?type=...
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //响应html:设置响应的Content-type，也就是设置响应体的内容
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter pw = resp.getWriter();//拿到响应对象里面的输出流

        //获取queryString中，type的值
        String type = req.getParameter("type");
        if ("1".equals(type)){//返回简单的html
            pw.println("<h3>获取网页成功</h3>");
        }else if("2".equals(type)){//返回复杂的动态html
            //html?type=2&username=xxx
            String username = req.getParameter("username");
            pw.println("<p>");
            pw.println("欢迎你," + username);
            pw.println("<p>");
        }
    }
}
